package top.ljlgithub.api.controller;

import com.mysql.jdbc.StringUtils;
import org.apache.ibatis.annotations.Delete;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.ljlgithub.api.bean.*;
import top.ljlgithub.api.common.response.Meta;
import top.ljlgithub.api.common.response.ResponseEntity;
import top.ljlgithub.api.exception.ParamException;
import top.ljlgithub.api.exception.RightException;
import top.ljlgithub.api.service.RoleService;
import top.ljlgithub.api.utils.RedisUtil;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName
 * @Description TODO
 * @Author L1470
 * @Date 2021/5/6 18:29
 * @Version 1.0
 **/
@RestController
@CrossOrigin
public class RoleController {

    @Autowired
    RoleService roleService;

    @RequiresPermissions("user:getAllRoles:*")
    @GetMapping("/roles")
    public ResponseEntity getRoleList(){
        Meta meta = new Meta("获取成功", 200);
        List<TreeRole> treeRole = roleService.getTreeRole();
        ResponseEntity<Object> result = new ResponseEntity<>(treeRole,meta);
        return result;
    }

    @RequiresPermissions("user:createRole:*")
    @PostMapping("/roles")
    public ResponseEntity addRole(@RequestBody RoleParam roleParam) throws ParamException {
        String roleName = roleParam.getRoleName();
        String roleDesc = roleParam.getRoleDesc();
        if (StringUtils.isNullOrEmpty(roleName) || StringUtils.isNullOrEmpty(roleDesc)){
            throw new ParamException();
        }
        Meta meta = new Meta("创建成功", 201);
        Role role = new Role(null, roleName, roleDesc);
        role = roleService.addRole(role);
        ResponseEntity<Object> result = new ResponseEntity<>(role,meta);
        return result;
    }

    @RequiresPermissions("user:getRoleById:*")
    @GetMapping("/roles/{id}")
    public ResponseEntity findRoleById(@PathVariable("id") int id){
        Meta meta = new Meta("获取成功", 200);
        Role role = roleService.findRole(id);
        ResponseEntity<Object> result = new ResponseEntity<>(role,meta);
        return result;
    }

    @RequiresPermissions("user:updateRole:*")
    @PutMapping("/roles/{id}")
    public ResponseEntity updateRole(@PathVariable("id") int id,@RequestBody RoleParam roleParam) throws ParamException {
        String roleName = roleParam.getRoleName();
        String roleDesc = roleParam.getRoleDesc();
        if (StringUtils.isNullOrEmpty(roleName)){
            throw new ParamException();
        }
        Meta meta = new Meta("更新成功", 200);
        Role role = new Role(id, roleName, roleDesc);
        role = roleService.updateRole(role);
        ResponseEntity<Object> result = new ResponseEntity<>(role,meta);
        return result;
    }

    @RequiresPermissions("user:deleteRoleRight:*")
    @DeleteMapping("/roles/{roleId}/rights/{rightId}")
    public ResponseEntity deleteRole(@PathVariable("roleId") int id,@PathVariable("rightId") String rightId){
        Meta meta = new Meta("删除权限成功", 200);
        String ids = roleService.findIdsById(id);
        String[] split = ids.split(",");
        List<String> strings = new ArrayList<>(Arrays.asList(split));
        strings.remove(rightId);
        split = strings.toArray(new String[]{});
        String join = String.join(",", split);
        List<TreeBO> treeBOS = roleService.updateRight(join, id);
        ResponseEntity<Object> result = new ResponseEntity<>(treeBOS,meta);
        return result;
    }

    @RequiresPermissions("user:updateRoleRight:*")
    @PostMapping("/roles/{roleId}/rights")
    public ResponseEntity updateRoleRight(@PathVariable("roleId") int id, @RequestBody RidsParam ridsParam) throws RightException {
        if (id == 0){
            throw new RightException();
        }
        String rids = ridsParam.getRids();
        Meta meta = new Meta("更新成功", 200);
        roleService.updateRight(rids,id);
        ResponseEntity<Object> result = new ResponseEntity<>(null,meta);

        return result;
    }

    @DeleteMapping("/roles/{id}")
    public ResponseEntity deleteRoleById(@PathVariable("id") Integer id) throws RightException {
        if (id == 0){
            throw new RightException();
        }
        roleService.deleteRole(id);
        Meta meta = new Meta("删除成功", 200);
        ResponseEntity<Object> result = new ResponseEntity<>(null,meta);
        return result;
    }
}
